package com.grace.cms.common.utils;

import java.nio.file.Paths;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;

import com.grace.common.enums.GraceServerRoot;
import com.grace.common.utils.StringUtils;

public class ThemeUtils {
    // 验证资源文件是否存在
    public static String getPath(String themeName, String errPath) {
        Resource resource = new FileSystemResource(
                Paths.get(GraceServerRoot.THEME_DIR.getWorkRoot(), themeName, errPath));
        if (!resource.exists() || !resource.isReadable()) {
            themeName = "default";
        }
        return StringUtils.format("/{}/{}", themeName, errPath);
    }

    public static String choosePath(HttpStatus status, String themeName) {
        switch (status) {
            case BAD_GATEWAY:
                return getPath(themeName, "/pages/error/403");
            case NOT_FOUND:
                return getPath(themeName, "/pages/error/404");
            case INTERNAL_SERVER_ERROR:
                return getPath(themeName, "/pages/error/500");
            default:
                return getPath(themeName, "/pages/error/404");
        }
    }

}
